package com.hm.controller;

import com.hm.pojo.HmAddress;
import com.hm.serivce.HmAddressService;
import com.hm.utils.HmResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(value = "地址管理接口",tags = "地址管理接口")
public class HmAddressController {

    @Autowired
    private HmAddressService hmAddressService;

    /**
     * 把本身来为2默认地址的地址修改为1
     * @param addid
     */
    @RequestMapping(value = "/UpdateStatus1",method = RequestMethod.GET)
    @ApiOperation(value = "选择收货地址页面")
    public HmResult UpdateStatus1(int[] addid){
        hmAddressService.UpdateStatus1(addid[0]);
        hmAddressService.UpdateStatus2(addid[1]);
        return HmResult.ok();
    }


    /**
     * 全查询地址列表
     * @return
     */
    @RequestMapping(value = "/addressList",method = RequestMethod.GET)
    @ApiOperation(value = "全查询地址列表")
    public List<HmAddress> addressList(int userid){
        List<HmAddress> list = hmAddressService.addressList(userid);
        return  list;
    }

    /**
     * 单查询地址列表
     * @param addid
     * @return
     */
    @RequestMapping(value = "/getaddressById",method = RequestMethod.POST)
    @ApiOperation(value="单查询地址列表")
    public HmAddress getaddressById(int addid){
        HmAddress hmAddress = hmAddressService.getaddressById(addid);
        return hmAddress;
    }

    /**
     * 新增地址操作
     * @param location
     * @param house_num
     * @param contacts
     * @param iphone
     */
    @RequestMapping(value = "/saveAddress",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value="新增地址操作")
    public HmResult saveAddress(String location, String house_num, String contacts, String iphone, HttpServletResponse response){
        hmAddressService.saveAddress(location,house_num,contacts,iphone);
        return HmResult.ok("添加地址成功");
    }

    /**
     * 删除地址操作
     * @param addid
     */
    @RequestMapping(value = "/deleteAddress",method = RequestMethod.GET)
    @ApiOperation(value="删除地址操作")
    public HmResult deleteAddress(int addid){
        hmAddressService.deleteAddress(addid);
        return HmResult.ok("删除地址成功");
    }

    /**
     * 修改地址操作
     * @param location
     * @param house_num
     * @param contacts
     * @param iphone
     */
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    @ApiOperation(value="修改地址操作")
    public HmResult updateAddress(int addid,String location,String house_num,String contacts,String iphone){
        hmAddressService.updateAddress(addid,location,house_num,contacts,iphone);
        return HmResult.ok("修改地址成功");
    }
}
